package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.constant.basic.SuperConstant;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.ConditionVO;
import com.itheima.sfbx.insurance.enums.ConditionEnum;
import com.itheima.sfbx.insurance.mapper.ConditionMapper;
import com.itheima.sfbx.insurance.pojo.CategoryCondition;
import com.itheima.sfbx.insurance.pojo.Condition;
import com.itheima.sfbx.insurance.pojo.InsuranceCondition;
import com.itheima.sfbx.insurance.service.ICategoryConditionService;
import com.itheima.sfbx.insurance.service.IConditionService;
import com.itheima.sfbx.insurance.service.IInsuranceConditionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description：筛选项服务实现类
 */
@Slf4j
@Service
public class ConditionServiceImpl extends ServiceImpl<ConditionMapper, Condition> implements IConditionService {

    @Autowired
    private ICategoryConditionService categoryConditionService;
    @Autowired
    private IInsuranceConditionService insuranceConditionService;
    private LambdaQueryWrapper<Condition> queryWrapper(ConditionVO conditionVO){
        LambdaQueryWrapper<Condition> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .eq(StrUtil.isNotBlank(conditionVO.getConditionKey()),Condition::getConditionKey,conditionVO.getConditionKey())
                .eq(StrUtil.isNotBlank(conditionVO.getDataState()),Condition::getDataState,conditionVO.getDataState())
                .like(StrUtil.isNotBlank(conditionVO.getConditionKeyName()),Condition::getConditionKeyName,conditionVO.getConditionKeyName());
        return wrapper;
    }


    @Override
    public Page<ConditionVO> findPage(ConditionVO conditionVO, int pageNum, int pageSize) {
        LambdaQueryWrapper<Condition> wrapper = queryWrapper(conditionVO);
        Page<Condition> page = this.page(new Page<>(pageNum, pageSize), wrapper);
        Page<ConditionVO> voPage = BeanConv.toPage(page, ConditionVO.class);
        return voPage;
    }

    @Override
    public ConditionVO findById(String conditionId) {
        Condition condition = this.getById(conditionId);
        if(condition==null){
            return null;
        }
        return BeanConv.toBean(condition, ConditionVO.class);
    }

    @Override
    @Transactional
    public ConditionVO save(ConditionVO conditionVO) {
        //查询筛选项编号是否存在
        Integer count = this.lambdaQuery()
                .eq(Condition::getConditionKey, conditionVO.getConditionKey())
                .eq(Condition::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        if(count>0){
            //存在
            throw new ProjectException(ConditionEnum.SAVE_FAIL);
        }
        Condition condition = BeanConv.toBean(conditionVO, Condition.class);
        this.save(condition);
        return BeanConv.toBean(condition, ConditionVO.class);
    }

    @Override
    @Transactional
    public Boolean update(ConditionVO conditionVO) {
        Condition condition = this.getById(conditionVO.getId());
        if(condition==null){
            throw new ProjectException(ConditionEnum.UPDATE_FAIL);
        }
        Integer count = this.lambdaQuery()
                .eq(Condition::getConditionKey, conditionVO.getConditionKey())
                .ne(Condition::getId, conditionVO.getId())
                .eq(Condition::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        if(count>0){
            throw new ProjectException(ConditionEnum.UPDATE_FAIL);
        }
        //判断原key是否和其他表有关联
        Integer count1 = categoryConditionService.lambdaQuery()
                .eq(CategoryCondition::getConditionKey, condition.getConditionKey())
                .eq(CategoryCondition::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        Integer count2 = insuranceConditionService.lambdaQuery()
                .eq(InsuranceCondition::getConditionKey, condition.getConditionKey())
                .eq(InsuranceCondition::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        if(count1>0||count2>0){
            throw new ProjectException(ConditionEnum.UPDATE_FAIL);
        }
        Condition condition1 = BeanConv.toBean(conditionVO, Condition.class);
        return this.updateById(condition1);
    }

    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        List<String> ids = Arrays.asList(checkedIds);
        List<Condition> list = this.lambdaQuery()
                .in(Condition::getId, ids)
                .list();
        if(CollUtil.isEmpty(list)){
            return true;
        }
        Set<String> collect = list.stream().map(Condition::getConditionKey).collect(Collectors.toSet());
        Integer count1 = categoryConditionService.lambdaQuery()
                .in(CategoryCondition::getConditionKey, collect)
                .eq(CategoryCondition::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        Integer count2 = insuranceConditionService.lambdaQuery()
                .in(InsuranceCondition::getConditionKey, collect)
                .eq(InsuranceCondition::getDataState, SuperConstant.DATA_STATE_0)
                .count();
        if(count1>0||count2>0){
            throw new ProjectException(ConditionEnum.UPDATE_FAIL);
        }
        return this.removeByIds(ids);
    }

    @Override
    public List<ConditionVO> findList(ConditionVO conditionVO) {
        LambdaQueryWrapper<Condition> wrapper = queryWrapper(conditionVO);
        List<Condition> list = this.list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        List<ConditionVO> voList = BeanConv.toBeanList(list, ConditionVO.class);
        return voList;
    }

    @Override
    public List<ConditionVO> findInConditionKeys(List<String> conditionKeys) {
        if(CollUtil.isEmpty(conditionKeys)){
            throw new ProjectException(ConditionEnum.LIST_FAIL);
        }
        List<Condition> list = this.lambdaQuery()
                .in(Condition::getConditionKey, conditionKeys)
                .list();
        if(CollUtil.isEmpty(list)){
            return null;
        }
        List<ConditionVO> voList = BeanConv.toBeanList(list, ConditionVO.class);
        return voList;
    }
}
